import pykeyboard
import pymouse
import time
import os
import random
from code3.图像识别.trimage_match import ImageMatch

class ImageMatchTest():
    # 初始化鼠标对象和键盘对象
    def __init__(self):
        self.mouse = pymouse.PyMouse()
        self.keyboard = pykeyboard.PyKeyboard()
        self.match = ImageMatch()

    #获取坐标
    def pos(self, template_name):
        x, y = self.match.find_image(template_name)
        return x, y

    #执行指定坐标左键单击
    def do_click(self, template_name):
        x, y = self.pos(template_name)
        self.mouse.move(x, y)
        self.mouse.click(x, y, button=1, n=1)
        time.sleep(2)
        print('在坐标[%d, %d]处执行单击操作'%(x, y))
    #
    # #执行左键双击操作
    # def rand_double_click(self, template_name):
    #     x, y = self.pos()
    #     self.mouse.move(x, y)
    #     self.mouse.click(x, y, button=1, n=2)
    #     time.sleep(2)
    #     print('在坐标[%d, %d]处执行双击操作'%(x, y))
    #
    # #执行右键单击
    # def rand_right_click(self):
    #     x, y = self.pos()
    #     self.mouse.move(x, y)
    #     self.mouse.click(x, y, button=2, n=1)
    #     time.sleep(2)
    #     print('在坐标[%d, %d]处执行右键单击操作'%(x, y))
    #
    # #执行下拉框选择
    # def rand_select(self, count):
    #     x, y = self.pos()
    #     self.mouse.move(x, y)
    #     self.mouse.click(x, y)
    #     for i in range(count):
    #         self.keyboard.press_key(self.keyboard.down_key)
    #         self.keyboard.release_key(self.keyboard.down_key)
    #     self.keyboard.press_key(self.keyboard.enter_key)
    #     self.keyboard.release_key(self.keyboard.enter_key)
    #     time.sleep(2)
    #     print('在坐标[%d, %d]处执行下拉框操作'%(x, y))
    #
    # #执行随机字符串输入
    # def rand_input(self):
    #     x, y = self.pos()
    #     self.mouse.move(x, y)
    #     self.mouse.click(x, y)
    #     str_list = ['hello world', '蜗牛学院', '你瞅啥', '瞅你咋滴', '再瞅一个试试', '试试就试试', 'OK', 2345]
    #     rand_str = random.choice(str_list)
    #     self.keyboard.type_string(rand_str)
    #     time.sleep(2)
    #     print('在坐标[%d, %d]处执行字符串%s输入操作'%(x, y, rand_str))
    #
    # #执行随机按键
    # def rand_key(self):
    #     x, y = self.pos()
    #     self.mouse.move(x, y)
    #     key_list = [self.keyboard.alt_key, self.keyboard.enter_key, self.keyboard.up_key, self.keyboard.down_key, self.keyboard.cancel_key]
    #     rand_key = random.choice(key_list)
    #     self.keyboard.press_key(rand_key)
    #     self.keyboard.release_key(rand_key)
    #     time.sleep(2)
    #     print('在坐标[%d, %d]处执行按键%s操作'%(x, y, rand_key))

if __name__ == '__main__':
    match = ImageMatchTest()
    os.system('start /b calc.exe')
    time.sleep(2)

    match.do_click('1.png')

    match.do_click('add.png')

    match.do_click('2.png')

    match.do_click('equal.png')
    if ImageMatch().check_exsit('result.png'):
        print('测试成功!')
    else:
        print('测试失败!')

    os.system("taskkill -f -IM calc.exe")

